package com.kellycheung.chicken_go_go;

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Canvas;
import android.graphics.Paint;

public class TimerObj extends GameObject {
	public enum TimerType {
		countDown(-1), counting(1);
		
		@SuppressWarnings("unused")
		private final int iVal;
		
		private TimerType( int p_iVal ) {
			this.iVal = p_iVal;
		}
	}
	
	protected TimerType ttType;
	protected int iCntFm;
	protected int iCntTo;
	protected int iCntNow;
	protected int iCntLen;
	protected Timer tCntTimer;
	protected boolean isShowing = true;
	
	public TimerObj( GameScene p_gsScene , int p_iZOrder , TimerType p_tt , int p_iDuration , GOAnchor p_goa ) {
		this( p_gsScene , p_iZOrder , p_tt , p_iDuration , p_goa , 0f , 0f);
	}
	
	public TimerObj( GameScene p_gsScene , int p_iZOrder , TimerType p_tt , int p_iDuration , GOAnchor p_goa , float p_fX , float p_fY) {
		super( p_gsScene , p_iZOrder , p_goa , p_fX , p_fY );
		// TODO Auto-generated constructor stub
		
		try {
			this.ttType = p_tt;
			
			switch ( this.ttType ) {
			case countDown:
				this.iCntFm = p_iDuration;
				this.iCntNow = this.iCntFm; 
				this.iCntTo = 0;
				this.iCntLen = String.valueOf(this.iCntFm).length();
				break;
			case counting:
				this.iCntTo = p_iDuration;
				this.iCntNow = this.iCntTo; 
				this.iCntFm = 0;
				this.iCntLen = String.valueOf(this.iCntTo).length();
				break;
			}
			
			this.tCntTimer = new Timer();
			
			this.tCntTimer.scheduleAtFixedRate( new TimerTask() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					if ( isAllowRun() && !isTimesUp() ) {
						switch ( ttType ) {
						case countDown:
							if ( iCntNow > iCntTo ) {
								iCntNow--;
							}
							break;
						case counting:
							if ( iCntNow < iCntTo || iCntTo != -1 ) {
								iCntNow++;
							}
							break;
						}
					}
				}
			} , 0 , 1000 );
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "TimerObj" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	@Override
	public void destroy() {
		try {
			if ( this.tCntTimer != null ) {
				this.tCntTimer.cancel();
				this.tCntTimer.purge();
				this.tCntTimer = null;
			}
			
			super.destroy();
		} catch ( Exception p_e ) {
			// TODO Auto-generated catch block
			GameUtil.LogMsg( this , "TimerObj.destroy" );
			GameUtil.ErrorHandler( this , p_e );
		}
	}
	
	public boolean isTimesUp() {
		if ( this.iCntNow == this.iCntTo || this.iCntTo == -1 ) {
			return true;
		}
		
		return false;
	}
	
	public float getTimerWidth() {
		return this.getWidth() * this.iCntLen;
	}

	public float getTimerHeight() {
		return this.getHeight();
	}

	@Override
	public void draw( Canvas p_c , Paint p_p ) {
		if ( this.isShowing ) {
		GCoordinate gcOrig = new GCoordinate( this.gcCoord );
		GRectangle gr = new GRectangle( this.gcCoord.fX + this.getTimerWidth() , this.gcCoord.fY , this.gcCoord.fX , this.gcCoord.fY + this.getTimerHeight() ).convCoord( GOAnchor.DownRight ).convCoord( this.goaAnchor );
		float iNewX;
		
		for ( int i = 0 ; i < this.iCntLen ; i++ ) {
			iNewX = gr.fLeft + (this.getWidth() * i);
			
			this.setCoord( iNewX , gr.fUp );
			
			if ( (i - (this.iCntLen-String.valueOf(this.iCntNow).length())) <0 ) {
				this.setAniImgResCur( 0 );
			}
			else {
				this.setAniImgResCur( Integer.valueOf( String.valueOf( String.valueOf(this.iCntNow).charAt( i - (this.iCntLen-String.valueOf(this.iCntNow).length()) ) ) ) );
			}
			
			super.draw( p_c , p_p );
		}
		
		this.gcCoord.set( gcOrig );
		}
	}
}
